package com.hh.system.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hh.baseframework.util.Convert;
import com.hh.hibernate.dao.inf.IHibernateDAO;
import com.hh.system.bean.HhSysParam;
import com.hh.system.util.SysParam;

@Service
public class DataInitializationService {
	@Autowired
	private IHibernateDAO<HhSysParam> sysParamDao;

	@Transactional
	public void dataInitialization() throws IOException {
		createData();
		initSysStaticVar();
		initProperties();
	}

	private void initProperties() throws IOException {
		Properties p = new Properties();
		ClassLoader classloader = Thread.currentThread()
				.getContextClassLoader();
		InputStream in = classloader.getResourceAsStream("system.properties");
		p.load(in);
		SysParam.COMMON_PROJECT_NAME = p.getProperty("COMMON_PROJECT_NAME");
	}

	private void initSysStaticVar() {
		List<HhSysParam> hhSysParamList = sysParamDao
				.queryList(HhSysParam.class);
		Map<String, HhSysParam> hhSysParamMap = Convert.listToMap(
				hhSysParamList, "getKey");
		HhSysParam hhSysParam = hhSysParamMap
				.get("Open_DataBase_SQL_monitoring");
		if (hhSysParam != null) {
			SysParam.Open_DataBase_SQL_monitoring = hhSysParam.getValue();
		}
		hhSysParam = hhSysParamMap.get("Open_Log_SQL_monitoring");
		if (hhSysParam != null) {
			SysParam.Open_Log_SQL_monitoring = hhSysParam.getValue();
		}
	}

	private void createData() {
		HhSysParam Whether_the_initial_data = sysParamDao.findEntityByPK(
				HhSysParam.class, "222cdc90-ca1f-4229-a12a-cf2ca77fbe1d");
		if (Whether_the_initial_data == null) {
			Whether_the_initial_data = new HhSysParam();
			Whether_the_initial_data
					.setId("222cdc90-ca1f-4229-a12a-cf2ca77fbe1d");
			Whether_the_initial_data.setKey("Whether_the_initial_data");
			Whether_the_initial_data.setValue("1");
			Whether_the_initial_data
					.setRemark("服务器启动要不要初始化数据。<br>【如果要启动会比较慢，初始化一次数据就可以了】<br>值为 1：初始化 &nbsp;&nbsp;&nbsp; 0：不初始化<br>");
			sysParamDao.createEntity(Whether_the_initial_data);
		}
		if ("1".equals(Whether_the_initial_data.getValue())) {
			this.dataInitializationImpl();
		}
	}

	@Transactional
	public void dataInitializationImpl() {

		HhSysParam Open_Log_SQL_monitoring = new HhSysParam();
		Open_Log_SQL_monitoring.setId("99379a43-7c51-4129-bbd3-52e08302b742");
		Open_Log_SQL_monitoring.setKey("Open_Log_SQL_monitoring");
		Open_Log_SQL_monitoring.setValue("1");
		Open_Log_SQL_monitoring.setRemark("开启日志SQL监控");
		sysParamDao.saveOrUpdateEntity(Open_Log_SQL_monitoring);

		HhSysParam Open_DataBase_SQL_monitoring = new HhSysParam();
		Open_DataBase_SQL_monitoring
				.setId("e145ce25-9aec-41f2-b1e2-f0aa8faa2dda");
		Open_DataBase_SQL_monitoring.setKey("Open_DataBase_SQL_monitoring");
		Open_DataBase_SQL_monitoring.setValue("1");
		Open_DataBase_SQL_monitoring.setRemark("开启数据库SQL监控");
		sysParamDao.saveOrUpdateEntity(Open_DataBase_SQL_monitoring);

	}
}
